package 尊享面试.哈希;

import java.util.HashMap;
import java.util.Map;

public class a266回文排列 {
    static class Solution {
        public boolean canPermutePalindrome0(String s) {
            HashMap<Character, Integer> map = new HashMap<>();
            for (int i = 0; i < s.length(); i++) {
                map.put(s.charAt(i), map.getOrDefault(s.charAt(i), 0) + 1);
            }
            int odd = 0; // 偶数
            int even = 0; // 奇数
            for (Map.Entry<Character, Integer> entry : map.entrySet()) {
                Integer val = entry.getValue();
                if (val % 2 == 0) {
                    odd++;
                } else {
                    even++;
                }
            }
            return even == 1 || (even == 0 && odd != 0);
            // return even <=1;
        }

        public boolean canPermutePalindrome(String s) {
            int hash = 0;
            System.out.println(Integer.toBinaryString(hash));
            for (char ch : s.toCharArray()) {
                hash ^= 1 << (ch - 'a');
                System.out.println(Integer.toBinaryString(hash));
            }
            System.out.println("==========");
            int even = 0;
            while (hash > 0) {
                System.out.println("a: " + Integer.toBinaryString(hash));
                System.out.println("b: " + Integer.toBinaryString(hash - 1));
                hash &= hash - 1;
                even++;
                System.out.println("c " + Integer.toBinaryString(hash));
            }
            return even <= 1;
        }
    }

    public static void main(String[] args) {
        Solution solution = new Solution();
        System.out.println(solution.canPermutePalindrome("cad"));
        System.out.println(16 & 15);
    }
}
